import pytest
import allure
from selenium import webdriver

@pytest.fixture()
def browser():
    global driver

    # chrome_options = webdriver.ChromeOptions()
    # chrome_options.add_argument('--headless')
    # driver = webdriver.Remote(
    #     command_executor='http://192.168.237.1:4444',
    #     options=chrome_options,
    # )

    #无头模式
    # chrome_options = Options()
    # chrome_options.add_argument('--headless')

    #一般模式
    driver = webdriver.Chrome()

    yield driver


#截图函数
@pytest.hookimpl(hookwrapper=True)
def pytest_runtest_makereport():
    # 获取钩子方法的调用结果
    out = yield
    """
        setup:
        call:测试执行中
        teardown:
        wasxfail:标记失败用例
    """
    report = out.get_result()
    #表示在用例执行中，如果report有wasxfail的属性，就表示用例被标记为xfail
    if report.when == 'call':
        xfail = hasattr(report, 'wasxfail')
        #表示用例跳过
        if (report.skipped and xfail) or (report.failed and not xfail):
            with allure.step("失败截图"):
                allure.attach(driver.get_screenshot_as_png(), "失败截图", allure.attachment_type.PNG)